//
//  PopoverAnimation.swift
//  DSWeibo
//
//  Created by J on 16/2/22.
//  Copyright © 2016年 J. All rights reserved.
//  负责转场动画

import UIKit

//定义常量保存通知名称
let XMGPopoverAnimationWillShow    = "XMGPopoverAnimationWillShow"
let XMGPopoverAnimationWillDismiss = "XMGPopoverAnimationWillDismiss"



class PopoverAnimation: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning{
    
    
    //记录当前是否展开
    var isPresent: Bool = false
    
    //定义属性保存菜单大小
    var presentFrame = CGRectZero
    
    //实现代理方法
    ///  用于创建负责转场动画的对象
    ///
    ///  - parameter presentedViewController:  被展现的控制器
    ///  - parameter presentingViewController: 发起的控制器 Xcode6是nil Xcode7时野指针
    ///
    ///  - returns: 负责转场动画的对象
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?{
        
        let pc = PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
        //设置菜单大小
        pc.presentFrame = presentFrame
        return pc
        
    }
    
    
    
    
    //MARK:只要实现了以下方法 系统自带的动画都会消失 需要程序员自己实现动画效果
    
    //告诉系统谁来负责modal的展现动画
    ///  - parameter presentedViewController:  被展现的控制器
    ///  - parameter presentingViewController: 发起的控制器
    //return 谁来负责
    
    //开始动画的时候调用一次
    internal func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isPresent = true
        //发送通知, 通知控制器即将展开

        NSNotificationCenter.defaultCenter().postNotificationName(XMGPopoverAnimationWillShow, object: nil)
        return self
    }
    
    //告诉系统谁来负责modal的消失动画
    //dismissed被关闭视图
    //return 谁来负责
    
    //结束动画的时候调用一次
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        //发送通知, 通知控制器即将消失
        NSNotificationCenter.defaultCenter().postNotificationName(XMGPopoverAnimationWillDismiss, object: nil)

        
        return self
    }
    
    
    //transitionContext 返回动画时长 保存了动画需要的所有参数
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval{
        return 0.5
    }
    
    
    //告诉系统如何动画 无论 展现还是消失都会调用这个方法   一个动画调用2次
    //transitionContext 返回动画时长 保存了动画需要的所有参数
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning){
        
        if isPresent {
            //1.拿到展现视图

            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!//拿到需要展现的视图
            toView.transform = CGAffineTransformMakeScale(1.0, 0.0)//高度为0
            
            //注意一定要将视图添加到容器上
            transitionContext.containerView()?.addSubview(toView)
            //设置⚓️点
            toView.layer.anchorPoint = CGPoint(x:0.5,y:0 )//默认中间展开 ⚓️点0.5,0.5 要从上面展开设置x:0.5
            //执行动画 transitionDuration(transitionContext)统一动画时长
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                
                toView.transform = CGAffineTransformIdentity
                
                }) { (_) -> Void in
                    //执行动画完毕告诉系统
                    //如果不写可能导致一些未知的错误
                    transitionContext.completeTransition(true)
            }
        }else{//关闭动画
            
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
            
            //从下至上的缩回去
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                
                //注意:由于CGFloat是不准确的 如果写0.0会没有动画
                fromView.transform = CGAffineTransformMakeScale(1.0, 0.000001)//高度为0
                
                }) { (_) -> Void in
                    //执行动画完毕告诉系统
                    //如果不写可能导致一些未知的错误
                    transitionContext.completeTransition(true)
            }
            
            
            
        }
        
    }
}
